<template>
  <div class="container">
    <!--  新增目录 -->
    <el-form
      :model="twoValue"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="所属学科" prop="subjectID" v-show="!this.$route.query.id && !this.$route.query.name">
        <el-select v-model="twoValue.subjectID" placeholder="请选择">
          <el-option v-for="item,index in disciplineList" :key="index" :label="item.label"  :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标签名称" prop="tagName">
        <el-input
          v-model="twoValue.tagName"
          placeholder="请输入标签名称"
          style="width: 89%"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button @click="cancel" >取消</el-button>
        <el-button type="primary" @click="onSubmit"
          >确定</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// 导入接口
import { simple } from '@/api/hmmm/subjects.js'

import { add, detail, update } from '@/api/hmmm/tags.js'
export default {
  // 父传子
  props: {
    isEdit: {
      type: [Boolean], // 类型
      required: true // 必传值
    },
    id: {
      type: [Number, String], // 类型
      required: true// 必传值
    }
  },
  name: 'bbb',
  // 钩子函数
  created () {
    this.disciplineListApi()
  },
  data () {
    return {
      twoValue: {
        subjectID: this.$route.query.id, // 下拉框的值
        tagName: null// 文本内容
      },
      //   学科列表
      disciplineList: [],

      // 验证规则
      rules: {
        subjectID: [{ required: true, message: '请输入标签名称', trigger: 'blur' }],
        tagName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 获取学科列表
    async disciplineListApi () {
      const res = await simple(this.query)
      console.log(res)
      this.disciplineList = res.data
      if (this.isEdit === true) {
        const ser = await detail({ id: this.id })
        this.twoValue.subjectID = ser.data.subjectID
        this.twoValue.tagName = ser.data.tagName
      }
    },
    // 点击取消
    cancel () {
      this.twoValue = {
        subjectID: '',
        tagName: ''
      }
      // 关闭弹窗
      this.$emit('close')
      //   this.$refs.ruleForm = ''
    },
    // 点击确定新增标签
    onSubmit () {
      this.$refs.ruleForm.validate(async (valid) => {
        //   做兜底验证
        if (valid) {
          // 点击修改按钮后修改
          if (this.isEdit === true) {
            await update({ id: this.id, ...this.twoValue })
            this.$message.success('修改成功')
          } else {
            const co = await add({ // 新增
            // 拿到数据并且渲染页面
              tagName: this.twoValue.tagName,
              subjectID: +this.twoValue.subjectID
            })
            // console.log(co)
            // 拿到他的id 有数据为true 没有则为false
            if (co.data.id) {
              this.$message.success('新增成功')
            } else {
              this.$message.error('新增失败')
            }
          }
          // 关闭弹窗
          this.$emit('close')
        } else {
          // 返回
          return false
        }
      })
    }
  }
}
</script>

<style scoped lang='less'></style>
